package goodbaby.dklt.customerview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Li.z on 2016/5/8.
 * 考勤饼状图
 */
public class AttendancePieChart extends View{
    private int mWidth;
    private int mHeight;

    private int color1 = Color.parseColor("#3398CC");
    private int color2 = Color.parseColor("#99CC33");
    private int color3 = Color.parseColor("#CD9933");
    private int textColor = Color.parseColor("#ffffff");

    private Paint mPaint = new Paint();

    private Paint mTextPaint = new Paint();

    private RectF mRectF;

    private int[] values;
    private float[] sweepAngles;

    private int totalValues;

    private float startAnagle = -100f;



    public AttendancePieChart(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        initCofig(context, attrs);
    }

    public AttendancePieChart(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initCofig(context, attrs);
    }

    private void initCofig(Context context, AttributeSet attrs){
        mTextPaint = new Paint();
        mTextPaint.setTextSize(20);
        mTextPaint.setColor(textColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);

        mPaint = new Paint();
        mPaint.setColor(color1);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.makeMeasureSpec(widthMeasureSpec,MeasureSpec.getMode(widthMeasureSpec));
        mWidth = MeasureSpec.getSize(mWidth);
        mHeight = mWidth;

        mRectF = new RectF(0,0,mWidth,mHeight);

        setMeasuredDimension(mWidth,mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(values == null){
            mPaint.setColor(color1);
            canvas.drawArc(mRectF, startAnagle,120, true, mPaint);
            mPaint.setColor(color2);
            canvas.drawArc(mRectF, startAnagle+120,120+1, true, mPaint);
            mPaint.setColor(color3);
            canvas.drawArc(mRectF, startAnagle+120+120,120+1, true, mPaint);
        }else{
            mPaint.setColor(color1);
            canvas.drawArc(mRectF, startAnagle,sweepAngles[0], true, mPaint);
            mPaint.setColor(color2);
            canvas.drawArc(mRectF, startAnagle+sweepAngles[0],sweepAngles[1], true, mPaint);
            mPaint.setColor(color3);
            canvas.drawArc(mRectF, startAnagle+sweepAngles[0]+sweepAngles[1],sweepAngles[2], true, mPaint);
        }
    }

    /**
     * @param value1  实到
     * @param value2  请假
     * @param value3  其他
     */
    public void setValues(int value1,int value2,int value3){
        values = new int[]{value1,value2,value3};
        totalValues = values[0]+values[1]+values[2];
        sweepAngles = new float[]{360*(values[0]/(float)totalValues),360*(values[1]/(float)totalValues),360*(values[2]/(float)totalValues)};
        invalidate();
    }
}
